home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / cicnAnimDemo-c / cicnAnim.c next >
Encoding:
Text File  |  1994-12-04  |  7.3 KB  |  405 lines  |  [TEXT/MMCC]

  1. //• cicn animation demo by Steve Chamberlin.
  2.  
  3. //• cicnAnim.c
  4.  
  5. #include "QDOffscreen.h"
  6.  
  7. MenuHandle    appleMenu, fileMenu, editMenu, extraMenu;
  8.  
  9. //• Why won't it compile without this?
  10. void MyEnable (MenuHandle menu, short item, Boolean ok);
  11.  
  12. #define appleID 1
  13. #define fileID 256
  14. #define editID 257
  15. #define extraID 258
  16.  
  17. #define openItem 1
  18. #define    closeItem 2
  19. #define quitItem 4
  20.  
  21. #define WIDTH 200
  22. #define HEIGHT 150
  23. #define ITEMWIDTH 32
  24. #define ITEMHEIGHT 32
  25.  
  26. Rect        bRect, items [20], bbox;
  27. short        x [20], y [20], vx [20], vy [20], color [20], max = 5;
  28. GWorldPtr    offscreen;
  29. CIconHandle            cicn [12];
  30. PixPatHandle        myPat;
  31. Boolean                Is32Bit ();
  32.  
  33. WindowPtr        myWindow;
  34. CWindowRecord     wRecord;
  35. Rect            dragRect;
  36.  
  37. void SetUpWindow (void);
  38. void DrawMy (Boolean hilited);
  39. void SetUpMenus (void);
  40. void AdjustMenus (void);
  41. void MyEnable (MenuHandle menu, short item, Boolean ok);
  42. void HandleMenu (long mSelect);
  43. Boolean Is32Bit (void);
  44. void InitMacintosh (void);
  45. void HandleMouseDown (EventRecord * theEvent);
  46. void HandleEvent (void);
  47. short RandomInt (short range);
  48. void SetUpItems (void);
  49. void LoadSwill (void);
  50. void SetUpOffscreen (void);
  51. void DrawFrame (void);
  52. void Animate (void);
  53. void main (void);
  54.  
  55. void SetUpWindow ()
  56. {
  57.     dragRect = qd.screenBits.bounds;
  58.  
  59.     myWindow = GetNewCWindow (256, &wRecord, (WindowPtr) - 1L);
  60.     SetPort (myWindow);
  61. }
  62.  
  63. void DrawMy (Boolean hilited)
  64. {
  65.     //• put redraw window code here, if needed
  66. }
  67.  
  68. void SetUpMenus ()
  69. {
  70.     appleMenu = GetMenu (appleID);
  71.     fileMenu = GetMenu (fileID);
  72.     editMenu = GetMenu (editID);
  73.     extraMenu = GetMenu (extraID);
  74.     AddResMenu (appleMenu, 'DRVR');
  75.     InsertMenu (appleMenu, 0);
  76.     InsertMenu (fileMenu, 0);
  77.     InsertMenu (editMenu, 0);
  78.     InsertMenu (extraMenu, 0);
  79.     CheckItem (extraMenu, 1, true);
  80.     DrawMenuBar ();
  81. }
  82.  
  83. void AdjustMenus ()
  84. {
  85.     WindowPeek wp;
  86.     short kind;
  87.     Boolean DA;
  88.  
  89.     wp = (WindowPeek) FrontWindow ();
  90.     kind = wp ? wp->windowKind : 0;
  91.     DA = kind < 0;
  92.  
  93.     MyEnable (editMenu, 1, DA);
  94.     MyEnable (editMenu, 3, DA);
  95.     MyEnable (editMenu, 4, DA);
  96.     MyEnable (editMenu, 5, DA);
  97.     MyEnable (editMenu, 6, DA);
  98.  
  99.     MyEnable (fileMenu, openItem, ! ((WindowPeek) myWindow)->visible);
  100.     MyEnable (fileMenu, closeItem, DA || ((WindowPeek) myWindow)->visible);
  101. }
  102.  
  103. void MyEnable (MenuHandle menu, short item, Boolean ok)
  104. {
  105.     if (ok)
  106.         EnableItem (menu, item);
  107.     else
  108.         DisableItem (menu, item);
  109. }
  110.  
  111. void HandleMenu (long mSelect)
  112. {
  113.     short            i, menuID = HiWord (mSelect);
  114.     short            menuItem = LoWord (mSelect);
  115.     Str255        name;
  116.     GrafPtr        savePort;
  117.     WindowPeek    frontWindow;
  118.  
  119.     switch (menuID)
  120.     {
  121.         case    appleID:
  122.             if (menuItem == 1)
  123.             {
  124.                 Alert (9999, 0L);
  125.                 break;
  126.             }
  127.             else
  128.                 {
  129.                     GetPort (&savePort);
  130.                     GetItem (appleMenu, menuItem, name);
  131.                     OpenDeskAcc (name);
  132.                     SetPort (savePort);
  133.             }
  134.         break;
  135.     
  136.         case    fileID:
  137.             switch (menuItem)
  138.             {
  139.                 case    openItem:
  140.                     ShowWindow (myWindow);
  141.                     SelectWindow (myWindow);
  142.                 break;
  143.     
  144.                 case    closeItem:
  145.                     if ((frontWindow = (WindowPeek) FrontWindow ()) == 0L)
  146.                     break;
  147.                     if (frontWindow->windowKind < 0)
  148.                         CloseDeskAcc (frontWindow->windowKind);
  149.                     else if (frontWindow = (WindowPeek) myWindow)
  150.                             HideWindow (myWindow);
  151.                  break;
  152.      
  153.                  case    quitItem:
  154.                     ExitToShell ();
  155.                 break;
  156.             }
  157.         break;
  158.     
  159.         case    editID:
  160.             if (! SystemEdit (menuItem - 1))
  161.                 SysBeep (5);
  162.         break;
  163.  
  164.         case    extraID:
  165.             if (menuItem <= 4)
  166.             {
  167.                 for (i = 1; i < 5; i++)
  168.                     CheckItem (extraMenu, i, false);
  169.         
  170.                 max = menuItem * 5;
  171.                 CheckItem (extraMenu, menuItem, true);
  172.             }
  173.             if (menuItem == 6)
  174.                 SetUpItems ();
  175.                 break;
  176.     }
  177. }
  178.  
  179. Boolean Is32Bit ()
  180. {
  181.     SysEnvRec mySE;
  182.  
  183.     SysEnvirons (2, &mySE);
  184.  
  185.     if (! mySE.hasColorQD)
  186.         return (false);
  187.  
  188.     return (NGetTrapAddress (0xAB03, ToolTrap) !=
  189.             NGetTrapAddress (0xA89F, ToolTrap));
  190. }
  191.  
  192. void InitMacintosh ()
  193. {
  194.     MaxApplZone ();
  195.  
  196.     InitGraf (&qd.thePort);
  197.     InitFonts ();
  198.     FlushEvents (everyEvent, 0);
  199.     InitWindows ();
  200.     InitMenus ();
  201.     TEInit ();
  202.     InitDialogs (0L);
  203.     InitCursor ();
  204.  
  205. }
  206.  
  207. void HandleMouseDown (EventRecord * theEvent)
  208. {
  209.     WindowPtr    theWindow;
  210.     short            windowCode;
  211.  
  212.     windowCode = FindWindow (theEvent->where, &theWindow);
  213.     switch (windowCode)
  214.     {
  215.             case inSysWindow:
  216.                 SystemClick (theEvent, theWindow);
  217.             break;
  218.         
  219.             case inMenuBar:
  220.                 AdjustMenus ();
  221.                 HandleMenu (MenuSelect (theEvent->where));
  222.             break;
  223.         
  224.             case inDrag:
  225.                 if (theWindow == myWindow)
  226.                         DragWindow (myWindow, theEvent->where, &dragRect);
  227.             break;     
  228.         
  229.             case inContent:
  230.                 if (theWindow == myWindow)
  231.                 {
  232.                     if (theWindow != FrontWindow ())
  233.                         SelectWindow (myWindow);
  234.                     else
  235.                         InvalRect (&myWindow->portRect);
  236.                 }
  237.             break;
  238.  
  239.             case inGoAway:
  240.                 if (theWindow == myWindow &&
  241.                     TrackGoAway (myWindow, theEvent->where))
  242.                 HideWindow (myWindow);
  243.             break;
  244.     }
  245. }
  246.  
  247. void HandleEvent ()
  248. {
  249.     EventRecord    theEvent;
  250.  
  251.     HiliteMenu (0);
  252.     WaitNextEvent (everyEvent, &theEvent, 0L, 0L);
  253.     switch (theEvent.what)
  254.     {
  255.         case mouseDown:
  256.             HandleMouseDown (&theEvent);
  257.             break;
  258.         case keyDown:
  259.         case autoKey:
  260.             if ((theEvent.modifiers & cmdKey) != 0)
  261.             {
  262.                 AdjustMenus ();
  263.                 HandleMenu (MenuKey ((char) (theEvent.message & charCodeMask)));
  264.             }
  265.             break;
  266.         case updateEvt:
  267.             BeginUpdate (myWindow);
  268.             DrawMy (((WindowPeek) myWindow)->hilited);
  269.             EndUpdate (myWindow);
  270.             break;
  271.         case activateEvt:
  272.             InvalRect (&myWindow->portRect);
  273.             break;
  274.         }
  275. }
  276.  
  277. short RandomInt (short range)
  278. {
  279.     long rawResult;
  280.  
  281.     rawResult = Random ();
  282.  
  283.     if (rawResult < 0)
  284.         rawResult *= - 1;
  285.     
  286.     return ((rawResult * range) / 32768);
  287. }
  288.  
  289. void SetUpItems ()
  290. {
  291.     short i, n;
  292.  
  293.     for (i = 0; i < 20; i++)
  294.     {
  295.         x [i] = RandomInt (WIDTH - 2 * ITEMWIDTH) + ITEMWIDTH;
  296.         y [i] = RandomInt (HEIGHT - 2 * ITEMHEIGHT) + ITEMHEIGHT;
  297.         while ((n = RandomInt (13) - 6) == 0);
  298.         vx [i] = n;
  299.         while ((n = RandomInt (13) - 6) == 0);
  300.         vy [i] = n;
  301.         color [i] = RandomInt (12);
  302.         SetRect (&items [i], x [i], y [i], x [i] + ITEMWIDTH, y [i] + ITEMHEIGHT);
  303.     }
  304. }
  305.  
  306. void LoadSwill ()
  307. {
  308.     short i;
  309.  
  310.     for (i = 0; i < 12; i++)
  311.         cicn [i] = GetCIcon (256 + i);
  312.  
  313.     myPat = GetPixPat (256);
  314. }
  315.  
  316. void SetUpOffscreen ()
  317. {
  318.     GDHandle    saveGD;
  319.     GWorldPtr    saveGW;
  320.     QDErr        errcode;
  321.  
  322.     SetRect (&bRect, 0, 0, WIDTH, HEIGHT);
  323.  
  324.     GetGWorld (&saveGW, &saveGD);
  325.     errcode = NewGWorld (&offscreen, 8, &bRect, 0L, 0L, 0L);
  326.     if (errcode != noErr)        //• could not create new GWorld
  327.     {
  328.         SysBeep (1);
  329.         ExitToShell ();
  330.     }
  331.     LockPixels (offscreen->portPixMap);
  332.     SetGWorld (offscreen, 0L);
  333.     EraseRect (&bRect);
  334.     SetGWorld (saveGW, saveGD);
  335.     UnlockPixels (offscreen->portPixMap);
  336. }
  337.  
  338. void DrawFrame ()
  339. {
  340.     GDHandle     saveGD;
  341.     GWorldPtr    saveGW;
  342.  
  343.     GetGWorld (&saveGW, &saveGD);
  344.     LockPixels (offscreen->portPixMap);
  345.     SetGWorld (offscreen, 0L);
  346.     
  347.     FillCRect (&bRect, myPat);
  348.     Animate ();
  349.     
  350.     SetGWorld (saveGW, saveGD);
  351.     SetPort (myWindow);
  352.     
  353.     CopyBits ((BitMap*)*offscreen->portPixMap, 
  354.                &myWindow->portBits, 
  355.                &bRect,
  356.                &bRect, srcCopy, 0L);
  357.             
  358.     UnlockPixels (offscreen->portPixMap);
  359. }
  360.  
  361. void Animate ()
  362. {
  363.     short i;
  364.  
  365.     for (i = 0; i < max; i++)
  366.     {
  367.         PlotCIcon (&items [i], cicn [color [i]]);
  368.  
  369.         if (x [i] > WIDTH - ITEMWIDTH || x [i] < 0)
  370.             vx [i] *= - 1;
  371.         if (y [i] > HEIGHT - ITEMHEIGHT || y [i] < 0)
  372.             vy [i] *= - 1;
  373.         OffsetRect (&items [i], vx [i], vy [i]);
  374.         x [i] = items [i].left;
  375.         y [i] = items [i].top;
  376.     }
  377. }
  378.  
  379. void main ()
  380. {
  381.     short i;
  382.  
  383.     InitMacintosh ();
  384.     if (! Is32Bit ())     //• must have 32 bit Quickdraw
  385.     {
  386.         StopAlert (400, 0L);
  387.         ExitToShell ();
  388.     }
  389.     SetUpMenus ();
  390.     SetUpWindow ();
  391.  
  392.     GetDateTime ((unsigned long*) qd.randSeed);
  393.  
  394.     SetUpOffscreen ();
  395.     SetUpItems ();
  396.     LoadSwill ();
  397.  
  398.     for (; ; )
  399.     {
  400.         HandleEvent ();
  401.         DrawFrame ();
  402.     }
  403. }
  404.  
  405.